package transferobject;

import java.io.Serializable;
import java.util.Date;

/**
 * Transfer Object class for Item
 * @author Ilya_Poliakov
 */
public class Item implements Serializable {
	/**
	 * Serial version UID
	 */
	private static final long serialVersionUID = 1992308534038515994L;
	/**
	 * the amount by which the rate should exceed the current bid or start price
	 */
	private final float bidIncrement;
	/**
	 * Fixed price without auction flag
	 */
	private final boolean buyItNow;
	/**
	 * Item description
	 */
	private final String description;

	/**
	 * Item ID
	 */
	private int itemId;
	/**
	 * Item seller ID
	 */
	private final int sellerId;

	/**
	 * Start date and time of the auction
	 */
	private final Date startBiddingDate;
	/**
	 * Item start price
	 */
	private final float startPrice;
	/**
	 * Time before the end of the auction
	 */
	private final int timeLeft;
	/**
	 * Item title
	 */
	public String title;
	/**
	 * Create new item object
	 * @param itemId
	 * @param sellerId
	 * @param title
	 * @param description
	 * @param startPrice
	 * @param timeLeft
	 * @param startBiddingDate
	 * @param buyItNow
	 * @param bidIncrement
	 */
	public Item(int itemId, int sellerId, String title, String description,
			float startPrice, int timeLeft, Date startBiddingDate,
			boolean buyItNow, float bidIncrement) {
		super();
		this.itemId = itemId;
		this.sellerId = sellerId;
		this.title = title;
		this.description = description;
		this.startPrice = startPrice;
		this.timeLeft = timeLeft;
		this.startBiddingDate = startBiddingDate;
		this.buyItNow = buyItNow;
		this.bidIncrement = bidIncrement;
	}

	/**
	 * Create new Item object with unassigned ID
	 * @param sellerId
	 * @param title
	 * @param description
	 * @param startPrice
	 * @param timeLeft
	 * @param startBiddingDate
	 * @param buyItNow
	 * @param bidIncrement
	 */
	public Item(int sellerId, String title, String description,
			float startPrice, int timeLeft, Date startBiddingDate,
			boolean buyItNow, float bidIncrement) {
		this(-1, sellerId, title, description, startPrice, timeLeft,
				startBiddingDate, buyItNow, bidIncrement);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Item other = (Item) obj;
		if (Float.floatToIntBits(bidIncrement) != Float
				.floatToIntBits(other.bidIncrement))
			return false;
		if (buyItNow != other.buyItNow)
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (itemId != other.itemId)
			return false;
		if (sellerId != other.sellerId)
			return false;
		if (startBiddingDate == null) {
			if (other.startBiddingDate != null)
				return false;
		} else if (startBiddingDate.compareTo(other.startBiddingDate) != 0)
			return false;
		if (Float.floatToIntBits(startPrice) != Float
				.floatToIntBits(other.startPrice))
			return false;
		if (timeLeft != other.timeLeft)
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	/**
	 * return the bidIncrement
	 * @return the bidIncrement
	 */
	public float getBidIncrement() {
		return bidIncrement;
	}

	/**
	 * return the description
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * return the itemId
	 * @return the itemId
	 */
	public int getItemId() {
		return itemId;
	}

	/**
	 * return the sellerId
	 * @return the sellerId
	 */
	public int getSellerId() {
		return sellerId;
	}

	/**
	 * return the startBiddingDate
	 * @return the startBiddingDate
	 */
	public Date getStartBiddingDate() {
		return startBiddingDate;
	}

	/**
	 * return the startPrice
	 * @return the startPrice
	 */
	public float getStartPrice() {
		return startPrice;
	}

	/**
	 * return the timeLeft
	 * @return the timeLeft
	 */
	public int getTimeLeft() {
		return timeLeft;
	}

	/**
	 * return the title
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Float.floatToIntBits(bidIncrement);
		result = prime * result + (buyItNow ? 1231 : 1237);
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result + itemId;
		result = prime * result + sellerId;
		result = prime
				* result
				+ ((startBiddingDate == null) ? 0 : startBiddingDate.hashCode());
		result = prime * result + Float.floatToIntBits(startPrice);
		result = prime * result + timeLeft;
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	/**
	 * return the buyItNow
	 * @return the buyItNow
	 */
	public boolean isBuyItNow() {
		return buyItNow;
	}

	/**
	 * Set item ID
	 * @param itemId the itemId to set
	 */
	public void setItemId(int itemId) {
		this.itemId = itemId;
	}

}
